package com.davidrubio.main;

import in.wptrafficanalyzer.actionbarnavtab.R;

import java.util.ArrayList;

import android.app.ListFragment;
import android.os.Bundle;
import android.widget.TextView;

/** This is a listfragment class */
public class ExpensesFragment extends ListFragment {
	// static ArrayList<String> list = new ArrayList<String>();
	// static ArrayAdapter<String> adapter;
	// static ArrayList<String> dates = new ArrayList<String>();
	// static ArrayList<String> money = new ArrayList<String>();
	static ArrayList<RowList> rl = new ArrayList<RowList>();
	static RowListAdapter rla;
	static TextView mHeaderView;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		mHeaderView = (TextView) getActivity().getLayoutInflater().inflate(
				R.layout.header, null);
		if (rl.isEmpty()) {
			mHeaderView.setText("Introduce tu primer gasto");
		} else {
			mHeaderView.setText(getMinDate() + "-" + getMaxDate());

		}
		setListAdapter(null);
		getListView().addHeaderView(mHeaderView);

		rla = new RowListAdapter(getActivity().getLayoutInflater(),
				R.layout.itemlayout, rl);
		setListAdapter(rla);

		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onStart() {
		super.onStart();
		getListView().setBackgroundResource(R.color.tostadoclaro);
		getListView()
				.setDivider(getResources().getDrawable(R.drawable.divider));

		getListView().setDividerHeight(4);
		/** Setting the multiselect choice mode for the listview */
		// getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
	}

	public static void addItem(String date, String cnt) {
		if (!cnt.endsWith(".00")) {
			cnt = cnt.concat(".00");
		}
		rl.add(new RowList(date, cnt));
		rla.notifyDataSetChanged();
	}

	private String getMinDate() {
		String dateAux = null;
		for (RowList data : rl) {
			if (dateAux==null) {
				dateAux = data.getDate();
			} else {
				if (compareDates(dateAux, data.getDate()) == 1) {
					dateAux = data.getDate();
				}
			}
		}
		return dateAux;
	}

	private String getMaxDate() {
		String dateAux = null;
		for (RowList data : rl) {
			if (dateAux==null) {
				dateAux = data.getDate();
			} else {
				if (compareDates(dateAux, data.getDate()) == 2) {
					dateAux = data.getDate();
				}
			}
		}
		return dateAux;
	}

	private int compareDates(String date1, String date2) {
		String[] date_1 = date1.split("/");
		int[] date_1_i = new int[3];
		for (int i = 0; i < date_1_i.length; i++) {
			date_1_i[i] = Integer.parseInt(date_1[i]);
		}
		String[] date_2 = date2.split("/");
		int[] date_2_i = new int[3];
		for (int i = 0; i < date_2_i.length; i++) {
			date_2_i[i] = Integer.parseInt(date_2[i]);
		}
		int result = 0;
		if (date_1_i[2] > date_2_i[2]) {
			result = 1;
		} else if (date_1_i[2] < date_2_i[2]) {
			result = 2;
		} else {
			if (date_1_i[1] > date_2_i[1]) {
				result = 1;
			} else if (date_1_i[1] < date_2_i[1]) {
				result = 2;
			} else {
				if (date_1_i[0] > date_2_i[0]) {
					result = 1;
				} else if(date_1_i[0] < date_2_i[0]){
					result = 2;// son la misma fecha
				}else{
					result =0;
				}
			}
		}
		return result;
	}

}
